package DatabasePackage;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * This is the schema for user
 */
public class UserCoursesDBTable extends DBtable {
  public enum UserCoursesDBTableScheme {
    USER("varchar(255)"), COURSE("varchar(255)");
    private final String COLUMN_TYPE;
    
    UserCoursesDBTableScheme(String type) {
      COLUMN_TYPE = type;
    }

    String getType() {
      return COLUMN_TYPE;
    }
  }
  
  public UserCoursesDBTable() {
    super(new HashMap<String, String>(), new LinkedList<String>(), new LinkedList<String>());
    keys.add(UserCoursesDBTableScheme.USER.toString());
    keys.add(UserCoursesDBTableScheme.COURSE.toString());
    for (UserCoursesDBTableScheme column : UserCoursesDBTableScheme.values())
      scheme.put(column.toString(), column.getType());
  }
  
  /**
   * @param username
   *          the name of the user you wish to get the details of
   * @return returns a list of the fields of the selected user if it exists, if
   *         he doesn't throws an exception
   * @throws SQLException
   *           if there was problem connecting to the sql database or there was
   *           a database error
   * @throws ClassNotFoundException
   *           if connector/j was not linked
   * @throws ItemNotFoundException
   *           if there is no user with the given username
   */
  public List<String> getUserCourses(Connection con, String u) throws SQLException, ClassNotFoundException {
    Map<String, byte[]> conditions = new HashMap<String, byte[]>();
    conditions.put(UserCoursesDBTableScheme.USER.toString(), u.getBytes());
    List<Map<String,byte[]>> coursesMap = getItem(con, conditions);
    List<String> courses = new LinkedList<String>();
    for(Map<String,byte[]> userCourse : coursesMap){
      courses.add(new String(userCourse.get(UserCoursesDBTableScheme.COURSE.toString())));
    }
    return courses;
  }
  
  public List<String> getUsersByCourse(Connection con, String course) throws ClassNotFoundException, SQLException{
    Map<String, byte[]> conditions = new HashMap<String, byte[]>();
    conditions.put(UserCoursesDBTableScheme.COURSE.toString(), course.getBytes());
    List<Map<String,byte[]>> usersMap = getItem(con, conditions);
    List<String> users = new LinkedList<String>();
    for(Map<String,byte[]> userCourse : usersMap){
      users.add(userCourse.get(UserCoursesDBTableScheme.USER.toString()).toString());
    }
    return users;
  }
  

  /**
   * Deletes the given user from the database, if he doesn't exist, throws an
   * exception
   * 
   * @param name
   *          the username of the user to be deleted
   * @throws ItemNotFoundException
   *           if there is no user with the given username
   * @throws SQLException
   *           if there was problem connecting to the sql database or there was
   *           a database error
   * @throws ClassNotFoundException
   *           if connector/j was not linked
   */
  public void deleteUserCourseTuple(Connection con, String course, String u) throws ItemNotFoundException, SQLException, ClassNotFoundException {
    Map<String, byte[]> conditions = new HashMap<String, byte[]>();
    conditions.put(UserCoursesDBTableScheme.USER.toString(), u.getBytes());
    conditions.put(UserCoursesDBTableScheme.COURSE.toString(), course.getBytes()); 
    removeItem(con, conditions);
  }
  
  /**
   * Adds the given user to the database, if he does exist, throws an exception
   * 
   * @param u
   *          the user record to insert
   * @throws ItemAlreadyInDBException
   *           if there is already a user with the given username in the
   *           database
   * @throws SQLException
   *           if there was problem connecting to the sql database or there was
   *           a database error
   * @throws ClassNotFoundException
   *           if connector/j was not linked
   */
  public void insertUserCourseTuple(Connection con, String u, String course) throws ItemAlreadyInDBException, SQLException, ClassNotFoundException {
    Map<String, byte[]> record = new HashMap<String, byte[]>();
    record.put(UserCoursesDBTableScheme.USER.toString(), u.getBytes());
    record.put(UserCoursesDBTableScheme.COURSE.toString(), course.getBytes()); 
    insertItem(con, record);
  }

  public boolean tupleExist(Connection con, String u, String course) throws ClassNotFoundException, SQLException{
    Map<String, byte[]> record = new HashMap<String, byte[]>();
    record.put(UserCoursesDBTableScheme.USER.toString(), u.getBytes());
    record.put(UserCoursesDBTableScheme.COURSE.toString(), course.getBytes()); 
    List<Map<String, byte[]>> tup = getItem(con, record);
    if (tup.isEmpty()) return false;
    return true;
  }

public void deleteCourseStudents(Connection con, String name) throws ClassNotFoundException, SQLException, ItemNotFoundException {
	 Map<String, byte[]> conditions = new HashMap<String, byte[]>();
	    conditions.put(UserCoursesDBTableScheme.COURSE.toString(), name.getBytes()); 
	    removeItem(con, conditions);
}

}
